package com.peixian.system.controller;

import com.peixian.pojo.Result;
import com.peixian.pojo.StatusCode;
import com.peixian.system.service.UserService;
import com.peixian.util.RSACoderUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author KwokChiKit
 */
@Api(value = "登录控制器", tags = "登陆控制器")
@RestController
@CrossOrigin
@RequestMapping("/system/test")
public class TestController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "根据用户名获得公钥", notes = "根据用户名获得公钥")
    @GetMapping("/getPublicKey")
    public Result getPublicKey(@ApiParam("根据用户名获得公钥") @RequestParam String username) {
        return new Result(true,StatusCode.OK,"获取成功",123);
    }

    @ApiOperation(value = "根据公钥加密后的密码", notes = "根据公钥加密密码")
    @GetMapping("/getPass")
    public String getPassEncrypted(@ApiParam("公钥")@RequestParam("key") String key, @ApiParam("密码") @RequestParam("password") String password) throws Exception {
        byte[] bytes = Base64.decodeBase64(key.replaceAll(" ", "+"));
        return Base64.encodeBase64String(RSACoderUtils.encryptByPublicKey(password.getBytes(), bytes));
    }

    @PostMapping("/login")
    @ApiOperation(value = "传入用户名密码以登录", notes = "传入用户名密码以登录")
    public Result login(@RequestParam @ApiParam("用户名") String username,
                           @RequestParam @ApiParam("加密过的密码") String password) {
        return new Result(true,StatusCode.OK,"登录成功",userService.login(username, password));
    }

}
